/* start ready */


$(document).ready(function(){
	//用ESC关闭ajax窗口
	$($.browser.msie?document:window).keydown(function(e){$.kc_keydown(e.keyCode)});

});/* end ready */


(function ($){


$.kc_keydown=function(Key){//键盘响应
	switch(Key){
	case 27:
		$.kc_close();
		break;
	}
}
/**
 * 获得当前页地址
 */
$.kc_href=function(){
	var s=location.href;
	var num;
	num=s.indexOf('#');
	if(num>0) s=s.substring(0,num);
	num=s.indexOf('?');
	if(num>0) s=s.substring(0,num);
	return s;
}


$.kc_nbsp=function(n){
	var i,s='';
	for (i = 0; i < n; i++) {
		s+='<img src="/browse/home/images/none.gif" class="os nbsp"/>';
	}
	return s;
}

$.kc_ajax=function(r){
	//跳转
	if(r.GOTO!=undefined){
		location.href=r.GOTO;
		return false;
	}

	var url = r.URL == undefined ? $.kc_href() : r.URL;//传递页定义
	var cmd = r.CMD == undefined ? '' : r.CMD;//定义传递函数
	//显示LOADING方式,指定值则在对应的对象中显示，默认(0)浮动显示,1关闭显示
	var load = r.LOAD == undefined ? 0 : r.LOAD;
	var form = r.FORM == undefined ? '#k_form' : '#'+r.FORM;//读取值的表单域
	var ids = r.IDS;//单独指定的提交值
	var isc = r.ISCONFIRM;//是否进行验证
	var postdata=r;
	postdata['AJAX']=1;//告诉服务器是AJAX提交的
	postdata['HTTP_REFERER']= r['HTTP_REFERER']==undefined ? location.href :r['HTTP_REFERER'];//当前页地址
	postdata['URL']= url;
	var s_main;

	if (isc==undefined) {

		if (ids!=undefined) {
			var idss=ids.split(',');
			$.each(idss,function(i,n){
				postdata[n]=$('#'+n).val();
				//postdata+=(n+'='+$('#'+n).val())+'&';
			});
		}


		$(form+' input:text,'+form+' input:password,'+form+' input[type=hidden],'+form+' textarea').each(function(i,n){
			if(n.name) postdata[n.name]=$(n).val();
		});
		$(form+' input:radio:checked').each(function(i,n){
			if(n.name) postdata[n.name]=$(n).val();
		});
		var fields=$(form+' select').serializeArray();
		$.each(fields,function(i,n){
			if(n.name) postdata[n.name] = postdata[n.name]==undefined ? $(n).val() : postdata[n.name]+','+$(n).val();
		});
		$(form+' :checkbox:checked').each(function(i,n){
			if(n.name) postdata[n.name] = postdata[n.name]==undefined ? $(n).val() : postdata[n.name]+','+$(n).val();
		});

		var t=cmd.match(/^(delete|clear|close|logout)/);
		if(t){
			s_main='<table cellspacing="0" class="k_tip">';
			s_main+='<tr><th><img src="/browse/home/images/confirm_'+t[0]+'.gif"/></th><td>'+k_lang['confirm'][t[0]]+'</td></tr>';
			s_main+='</table>';
				
			var rr=postdata;
			rr['ISCONFIRM']=1;

			var s='';
			for(key in rr){
				s+=','+key+":'"+(rr[key].toString()).replace(/(\\|\')/g,'\\$1')+"'";
			}
			s='{'+s.substring(1)+'}';

			var s_but='<button onclick="$.kc_ajax('+s+')">'+k_lang.enter+'</button>';
			s_but+='<button onclick="$.kc_close()">'+k_lang.cancel+'</button>';
			var s_r={TITLE:k_lang['confirm']['title'],MAIN:s_main,BUTTON:s_but,WIDTH:400,HEIGHT:100};
			window.parent.$.kc_ajax_show(s_r);
			return;

		}
	}

	if (load==0) {//默认(0)浮动显示,1关闭显示
		if ($('#k_loading').length==0) {
			$('body').append('<div id="k_loading" style="position:absolute;top:0;left:0;z-index:9999">'+k_lang['loading']+'</div>')
		}
	}

	$.ajax({
		url:url,
		type:'POST',
		data:postdata,
		dataType:'json',
		timeout:60000,
		error:function(){
			$.kc_close();
			s_main='<table cellspacing="0" class="k_tip">';
			s_main+='<tr><th><img src="/browse/home/images/confirm_error.gif"/></th><td>'+k_lang['timeout']+'</td></tr>';
			s_main+='</table>';
			window.parent.$.kc_ajax_show({TITLE:'Error!',MAIN:s_main,BUTTON:'<button onclick="$.kc_close()">'+k_lang['enter']+'</button>',WIDTH:320,HEIGHT:100});
		},
		success: function(back){
			//提交操作的时候动作
			$('#k_loading').remove();
			$('.k_error').removeClass('k_error');

			var id=back.ID;
			if(id=='k_ajax'){
				window.parent.$.kc_ajax_show(back)
			}else{
				var main=back.MAIN;
				var js=back.JS;
				if(main!=undefined){
					$('#'+id).html(main);
					/**
					$('#'+id+' .k_ajax').attr('href','javascript:;').click(function(){
						$.kc_ajax(this.rel);
					});
					*/
				}

				if(js!=undefined){
					eval(js);
				}
			}
		}
	});

}

$.kc_ajax_show=function(r){//构造ajax交互窗口
	var title=r.TITLE;
	var main=r.MAIN;
	var but=r.BUTTON;
	var width=r.WIDTH;
	var height=r.HEIGHT;
	var js=r.JS;
	// 赋值并显示
	if(title!=undefined||main!=undefined){
		$('html').css('overflow','hidden');

		var w=$(window).width();
		var h=$(window).height();

		if($.browser.opera){//opera下$(window).height 并不是可是区域大小,不知道是不是jQuery的bug?
			h=document.documentElement.clientHeight;
		}

		if ($('#k_ajax').length==0) {
			$(document.body).append('<div id="k_ajax"></div>');
		}
		var str='<div id="k_ajaxBg"></div>';
		str+='<form id="k_ajaxForm">';
		str+='<div id="k_ajaxBox">';
		str+='<div id="k_ajaxTitle"><strong id="k_ajaxTtitle"></strong><a onclick="$.kc_close()" href="javascript:;"></a></div>';
		str+='<div id="k_ajaxMain">';
		str+='<div id="k_ajaxContent"></div>';
		str+='</div>';
		str+='<div id="k_ajaxSubmit"></div>';
		str+='</div></form>';


		$('#k_ajax').html(str).show();

		$('#k_ajaxBg').css({
			'top':document.documentElement.scrollTop,
			'width':w+30,
			'height':h+30,
			'left':document.documentElement.scrollLeft
		});
		$('#k_ajaxTitle').width(width);
		$('#k_ajaxTtitle').width(width-$('#k_ajaxTitle a.k_close').width()-55).html(title);
		$('#k_ajaxMain').css({width:width,height:height});
		$('#k_ajaxContent').html(main);
		$('#k_ajaxSubmit').width(width-5).html('<p>'+but+'</p>');
		var nHeight=height+$('#k_ajaxTitle').height()+$('#k_ajaxSubmit').height();//完整的高度

		$('#k_ajaxBox').css({
		  'left':document.documentElement.scrollLeft+(w-width)/2,
		  'width':width,
		  'height':nHeight,
		  'zindex':'901',
		  'position':'absolute',
		  'top':document.documentElement.scrollTop+((h-nHeight)/2)
		});

		$(window).resize(function(){
			w=$(window).width();
			h=$(window).height();

			height=$('#k_ajaxMain').height();
			nHeight=height+$('#k_ajaxTitle').height()+$('#k_ajaxSubmit').height();//完整的高度

			if($.browser.opera){
				h=document.documentElement.clientHeight;
			}

			$('#k_ajaxBg').css('width',w+30).css('height',h+30);
			$('#k_ajaxBox').css('top',document.documentElement.scrollTop+((h-nHeight)/2)).css('left',document.documentElement.scrollLeft+(w-width)/2);

		}).scroll(function(){//Opera下隐藏滚动条的情况下还能上下拖动..
			w=$(window).width();
			h=$(window).height();

			height=$('#k_ajaxMain').height();
			nHeight=height+$('#k_ajaxTitle').height()+$('#k_ajaxSubmit').height();//完整的高度

			$('#k_ajaxBg').css('width',w+30).css('height',h+30).css('top',document.documentElement.scrollTop).css('left',document.documentElement.scrollLeft);
			$('#k_ajaxBox').css('top',document.documentElement.scrollTop+((h-nHeight)/2)).css('left',document.documentElement.scrollLeft+(w-width)/2);
		});


		$('#k_ajaxContent input:first').focus();//焦点定位到第一个input框,IE7/8下失效?

		var tipp=$('#k_ajax p.k_err,#k_ajax p.k_ok');
		if(tipp.html()!=null){
			$('#k_ajax #k_ajaxMain').html('<table cellspacing="0" class="k_tip"><tr><th><img src="/browse/home/images/'+tipp.attr('class').substr(2,tipp.attr('class').length-2)+'.gif"/></th><td>'+tipp.html()+'</td></tr></table>');
		}
		$('#k_ajaxBox input').keydown(function(event){
			if(event.keyCode==13){
				$.kc_ajax($('#k_ajaxSubmit a.k_ajax:first').attr('rel'));//读取第一个k_ajax操作按钮的rel并调用$.kc_ajax
			}
		});

		$('#k_ajaxForm').submit(function(){
		  return false;
		});

	}

	if(js!=undefined){
		eval(js);
	}
}

/**
 * 返回ajax弹窗按钮
 */
$.kc_button=function(tit,r){
	var s_main,key,s='';
	for(key in r){
		s+=','+key+":'"+(r[key].toString()).replace(/(\\|\')/g,'\\$1')+"'";
	}
	s='{'+s.substring(1)+'}';
	s_main='<button onclick="$.kc_ajax('+s+')">'+tit+'</button>';
	return s_main;
}
/**
 * 关闭弹窗
 */
$.kc_close=function(){//隐藏ajax
	//$('html').css('overflow','');
	$('#k_ajax').empty();
	$('#k_loading').remove();
	$('html').css('overflow','auto');
}
/**
 * checkbox框
 */
$.kc_list=function(id,gray){
	var s;
	s = gray==1
	? '<input disabled="true" name="list_disabled" id="list_'+id+'" type="checkbox" value="'+id+'"/>'
	: '<input onclick="$.kc_list_click(this)" name="list" id="list_'+id+'" type="checkbox" value="'+id+'"/>';
	return s;
}
/**
 * checkbox框被点击的时候事件
 */
$.kc_list_click=function(obj){
	if($(obj).attr('checked')){
		$(obj).removeAttr('checked')
	}else{
		$(obj).attr('checked',true)
	}
}
/**
 * 控制checkbox框
 */
$.kc_list_select=function(obj,name){
	//全部选择
	if($(obj).attr('checked')){
		$('#k_table_'+name+' :checkbox[name=list]').attr('checked',true);
		$('#k_table_'+name+' tr').addClass('click');
	}else{
		$('#k_table_'+name+' :checkbox[name=list]').removeAttr('checked');
		$('#k_table_'+name+' tr').removeClass('click');
	}
}







/* 读取Cookie */
$.COOKIE=function(name){//读取Cookie,仿php的读取Cookie
	var sta=document.cookie.indexOf(name+"=");
	var len=sta+name.length+1;
	if((!sta)&&(name!=document.cookie.substring(0,name.length))){
		return null;
	}
	if(sta==-1) return null;
	var end=document.cookie.indexOf(';',len);
	if(end==-1) end=document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}
/* 设置Cookie */
$.setCookie=function(name,value,expires,path,domain,secure){
	var today=new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) + ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
	( ( path ) ? ';path=' + path : '' ) + ( ( domain ) ? ';domain=' + domain : '' ) + ( ( secure ) ? ';secure' : '' );
}


})(jQuery);

function go(url){
	parent.location=url;
}
